package com.dengke.springsecurity.controller;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.dengke.springsecurity.dao.ApplicantDAO;
import com.dengke.springsecurity.model.ApplicantInfo;
import com.dengke.springsecurity.validator.ApplicantValidator;

@Controller
@Transactional
// need to ues redirectAttributes
@EnableWebMvc
@RequestMapping("/form")
public class FormController {
	@Autowired
	private ApplicantDAO applicantDAO;
	@Autowired
	private ApplicantValidator applicantValidator;

	@RequestMapping("/")
	public String homePage(Model model) {
		return applicantList(model);
	}

	@RequestMapping("/applicantList")
	public String applicantList(Model model) {
		List<ApplicantInfo> list = applicantDAO.listApplicantInfos();
		model.addAttribute("applicantInfos", list);
		return "applicantList";
	}
	
	private Map<String, String>dataForPositons(){
		Map<String, String>positionMap=new LinkedHashMap<String,String>();
		positionMap.put("Developer", "Developer");
		positionMap.put("Leader", "Leader");
		positionMap.put("Tester", "Tester");
		return positionMap;
	}
	
	private List<String>dataForSkills(){
		List<String>list=new ArrayList<>();
		list.add("Java");
		list.add("C/C++");
		list.add("C#");
		return list;
	}
	
	private String formApplicant(Model model,ApplicantInfo applicantInfo){
		model.addAttribute("applicantForm", applicantInfo);
		Map<String, String>positionMap=this.dataForPositons();
		model.addAttribute("positionMap", positionMap);
		List<String>list=this.dataForSkills();
		model.addAttribute("skills", list);
		if (applicantInfo.getId()==null) {
			model.addAttribute("formTitle", "Create Applicant");
		}else {
			model.addAttribute("formTitle", "Edit Applicant");
		}
		return "formApplicant";
	}
	
	@RequestMapping("/createApplicant")
	public String createApplicant(Model model) {
		ApplicantInfo applicantInfo=new ApplicantInfo();
		return this.formApplicant(model, applicantInfo);
	}
	
	@RequestMapping("/editApplicant")
	public String editApplicant(Model model, @RequestParam("id") String id) {
		ApplicantInfo applicantInfo=null;
		if(id!=null){
			applicantInfo=this.applicantDAO.findApplicantInfo(id);
		}
		if (applicantInfo==null) {
			return "redirect:applicantList";
		}
		return this.formApplicant(model, applicantInfo);
	}
	
	
	 @RequestMapping("/deleteApplicant")
	   public String deleteApplicant(Model model, @RequestParam("id") String id) {
	       if (id != null) {
	           this.applicantDAO.deleteApplicant(id);
	       }
	       return "redirect:applicantList";
	   }
	 
	//set a form validator
	@InitBinder
	protected void initBinder(WebDataBinder	dataBinder) {
		Object target=dataBinder.getTarget();
		if (target==null) {
			return ;
		}
		System.out.println("Target="+target);
		if (target.getClass()==ApplicantInfo.class) {
			dataBinder.setValidator(applicantValidator);
		}
	}
	
	@RequestMapping(value="/saveApplicant",method=RequestMethod.POST)
	public String savaApplicant(Model model,@ModelAttribute("applicantForm") @Validated ApplicantInfo applicantInfo,
			BindingResult result,final RedirectAttributes redirectAttributes) {
		if (result.hasErrors()) {
			return this.formApplicant(model, applicantInfo);
		}
		this.applicantDAO.saveApplicant(applicantInfo);
		//important need @enablewebmvc
		//add message to flash scope
		redirectAttributes.addFlashAttribute("message", "Save Applicant Successful");
		return "redirect:applicantList";
	}
}
